<?php
/**
 * Created by PhpStorm.
 * User: 29620
 * Date: 2021/10/21
 * Time: 15:14
 */
namespace app\admin\model;
use think\Model;
use think\model\concern\SoftDelete;

class Brands extends BaseModel
{
    use SoftDelete;
    protected $table = 'ecm_brand';
    protected $pk = 'brand_id';
    //使用搜索器
    public function searchKeywordsAttr($q,$v,$d)
    {
        $q->where('brand_name','like','%'.$v.'%');
    }

    public function searchRecommendedAttr($q,$v,$d)
    {
        $q->where('recommended','=',$v);
    }

    public function searchIfShowAttr($q,$v,$d)
    {
        $q->where('if_show','in',$v);
    }

    public function searchBrandLogoAttr($q,$v,$d)
    {
        if ($v==1){
            $q->where('brand_logo','<>','');
        }else{
            $q->where(function ($q){
                $q->whereOr('brand_logo','=',null);
                $q->whereOr('brand_logo','=','');
            });

        }
    }

    public static function getBrand($where,$post)
    {
        $brand = Brands::withSearch(array_keys($where),$where)
            ->paginate(['list_rows'=>5,'query'=>$post]);
        if($brand->count() == 0){
            $brand = Brands::withSearch(array_keys($where),$where)
                ->paginate(['list_rows'=>5,'query'=>$post,'page'=>$brand->lastPage()]);
        }
        return $brand;
    }

    //查询软删的字段
    public static function geBrand($where,$post)
    {
        $brand = Brands::onlyTrashed()->withSearch(array_keys($where),$where)
            ->paginate(['list_rows'=>5,'query'=>$post]);
        if($brand->count() == 0){
            $brand = Brands::withSearch(array_keys($where),$where)
                ->paginate(['list_rows'=>5,'query'=>$post,'page'=>$brand->lastPage()]);
        }
        return $brand;
    }

        //public static function BrandAdd($data)
        //{
        //    try{
        //        $res = Brands::create($data);
        //        $msg = '添加成功';
        //    }catch (\Exception $e){
        //        $res = false;
        //        $msg = $e->getMessage();
        //    }
        //    return [$res,$msg];
        //}
        //
        //public static function BrandEdit($data,$id)
        //{
        //    try{
        //        $res = Brands::where('brand_id','=',$id)->update($data);
        //        $msg = '修改成功';
        //    }catch (\Exception $e){
        //        $res = false;
        //        $msg = '修改失败';
        //    }
        //    return [$res,$msg];
        //}
        //
        //public static function editBrand($id)
        //{
        //    $edit = Brands::where('brand_id','=',$id)->find()->toArray();
        //    return $edit;
        //}
}




























